﻿// <copyright file="Person.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2008-09-23</date>
// <summary>The Silverlight Application</summary>

namespace opensocial
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Browser;
    using System.Windows.Media.Imaging;
    using Synergist;

    /// <summary>
    /// A Person in an OpenSocial container
    /// </summary>
    public class Person
    {
        #region Constructors
        /// <summary>
        /// Initializes a new instance of the Person class.
        /// </summary>
        /// <param name="person">a person script object</param>
        internal Person(ScriptObject person)
        {
            this.ScriptObject = person;
        }
        #endregion

        #region Properties

        /// <summary>
        /// Gets the script object
        /// </summary>
        public ScriptObject ScriptObject { get; private set; }

        /// <summary>
        /// Gets a field as an array of strings
        /// </summary>
        /// <param name="fieldName">the field name</param>
        /// <returns>an enumerable collection of string</returns>
        public IEnumerable<string> GetStringArrayField(string fieldName)
        {
            var fieldValue = this.getField(fieldName) as ScriptObject;

            if (fieldValue == null)
            {
                return null;
            }

            double number = (double)fieldValue.GetProperty("length");

            int length = System.Convert.ToInt32(number);

            string[] fieldArray = new string[length];

            for (var i = 0; i < length; i++)
            {
                fieldArray[i] = fieldValue.GetProperty(i) as string;
            }

            return fieldArray;
        }

        #endregion

        #region Methods
        /// <summary>
        /// Gets the person id
        /// </summary>
        /// <returns>the person's ID</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "Mirroring OpenSocial API."), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "get", Justification = "Mirroring OpenSocial API.")]
        public string getId()
        {
            string id = this.ScriptObject.Invoke("getId") as string;

            return id;
        }

        /// <summary>
        /// Get the display name
        /// </summary>
        /// <returns>the display name</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "Mirroring OpenSocial API."), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "get", Justification = "Mirroring OpenSocial API.")]
        public string getDisplayName()
        {
            return this.ScriptObject.Invoke("getDisplayName") as string;
        }

        /// <summary>
        /// get a field value as a string
        /// </summary>
        /// <param name="key">the key to lookup</param>
        /// <returns>the value as a string</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "get", Justification = "Mirroring OpenSocial API."), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Calling the JS could cause an error.")]
        public object getField(string key)
        {
            if (this.ScriptObject == null)
            {
                return null;
            }

            try
            {
                var keyName = key.Eval();

                object value = this.ScriptObject.Invoke("getField", keyName);

                return value;
            }
            catch (System.Exception se)
            {
                System.Diagnostics.Debug.WriteLine(se);

                return null;
            }
        }

        /// <summary>
        /// returns true if the person is the viewer
        /// </summary>
        /// <returns>true if the person is the viewer</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "is", Justification = "Mirroring OpenSocial API.")]
        public bool isViewer()
        {
            return (bool)this.ScriptObject.Invoke("isViewer");
        }

        /// <summary>
        /// returns true if the person is the owner
        /// </summary>
        /// <returns>true if the person is the owner</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "is", Justification = "Mirroring OpenSocial API.")]
        public bool isOwner()
        {
            return (bool)this.ScriptObject.Invoke("isOwner");
        }

        /// <summary>
        /// Get the properties for the video
        /// </summary>
        /// <returns>all of the properties and their values.</returns>
        public override string ToString()
        {
            return Synergist.Utility.GetProperties(this);
        }

        #endregion

        /// <summary>
        /// Gets an image field
        /// </summary>
        /// <param name="key">the key in the person class</param>
        /// <returns>a BitmapImage or null</returns>
        public BitmapImage GetImageField(string key)
        {
            var uriString = this.getField(key) as string;

            if (uriString == null)
            {
                return null;
            }

            var uri = new Uri(uriString, UriKind.Absolute);

            return new BitmapImage(uri);
        }

        /// <summary>
        /// Gets a UriField
        /// </summary>
        /// <param name="key">the key for the person field</param>
        /// <returns>a Uri or null</returns>
        public Uri GetUriField(string key)
        {
            var uriString = this.getField(key) as string;

            if (uriString == null)
            {
                return null;
            }

            return new Uri(uriString, UriKind.Absolute);
        }

        /// <summary>
        /// Gets a field as a DateTime
        /// </summary>
        /// <param name="key">the key of the date field</param>
        /// <returns>a DateTime</returns>
        internal DateTime GetDateField(string key)
        {
            var date = this.getField(key);

            if (date == null)
            {
                return DateTime.MinValue;
            }

            return (DateTime)date;
        }
    }
}
